﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace QLTV.Forms.KhaiBao
{
    public partial class frmTrainingCourse : BaseForm
    {
        public frmTrainingCourse()
        {
            InitializeComponent();
            statusDataBar1.addTextChange(gridView1);
            statusDataBar1.AddControlPrint(gridControl1);
        }

        private void frmTrainingCourse_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dsTraining.tblRank' table. You can move, or remove it, as needed.
            this.tblRankTableAdapter.Fill(this.dsTraining.tblRank);
            // TODO: This line of code loads data into the 'dsTraining.tblTrainingCourse' table. You can move, or remove it, as needed.
            this.tblTrainingCourseTableAdapter.Fill(this.dsTraining.tblTrainingCourse);

        }

        private void statusDataBar1_NewClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            this.tblTrainingCourseBindingSource.AddNew();
        }

        private void statusDataBar1_UpdateClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            try
            {
                this.tblTrainingCourseBindingSource.EndEdit();
                this.tblTrainingCourseTableAdapter.Update(dsTraining.tblTrainingCourse);
                for (int i = 0; i < dsTraining.tblTrainingCourse.Rows.Count; i++)
                {
                    if (dsTraining.tblTrainingCourse.Rows[i]["RankList"] != null)
                    {
                        int courseID = (Int32)dsTraining.tblTrainingCourse.Rows[i]["nTrainingCourse"];
                        Database.dsTrainingTableAdapters.tblRankCourseTableAdapter tadRankCourse = new Database.dsTrainingTableAdapters.tblRankCourseTableAdapter();
                        tadRankCourse.DeleteFirst(courseID);
                        object[] listrank = dsTraining.tblTrainingCourse.Rows[i]["RankList"].ToString().Split(',');
                        foreach (var item in listrank)
                        {
                            if (item.ToString().Trim() != "")
                                    tadRankCourse.Insert(courseID, Convert.ToInt32(item));
                        }
                    }
                }


                this.tblTrainingCourseTableAdapter.Fill(this.dsTraining.tblTrainingCourse);
                alertControl1.Show(this, rm.GetString("sMessageCaption", myCultureInfo), rm.GetString("sMessageSuccess", myCultureInfo));
            }
            catch (Exception ex) { XtraMessageBox.Show(ex.ToString()); }
        }

        private void statusDataBar1_DeleteClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            if (XtraMessageBox.Show(rm.GetString("sConfirm", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    this.tblTrainingCourseBindingSource.RemoveCurrent();
                    this.tblTrainingCourseTableAdapter.Update(dsTraining.tblTrainingCourse);
                    this.tblTrainingCourseTableAdapter.Fill(this.dsTraining.tblTrainingCourse);
                }
                catch (SqlException ex)
                {
                    this.tblTrainingCourseTableAdapter.Fill(this.dsTraining.tblTrainingCourse);
                    if (ex.ErrorCode == -2146232060)
                    {
                        XtraMessageBox.Show(rm.GetString("sDataInUsed", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        XtraMessageBox.Show(ex.Message, rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }


        }

        private void statusDataBar1_UndoClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            this.tblTrainingCourseBindingSource.CancelEdit();
            this.tblTrainingCourseTableAdapter.Fill(this.dsTraining.tblTrainingCourse);
        }
    }
}